﻿using Inaction;
using Inaction.Core;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace Inaction
{
    public class TranstionChain : Collection<ISavable>
    {
        public void Save()
        {
            var items = RemotingMethod.Invoke<IEnumerable<ISavable>>(new Func<IEnumerable<ISavable>, IEnumerable<ISavable>>(DoSave), this.ToArray());
            foreach (var item in items)
            {
                var s = this.FirstOrDefault(p => p.Key == item.Key);
                s.Clone(item);
            }
        }

        private static IEnumerable<ISavable> DoSave(IEnumerable<ISavable> items)
        {
            using (var connection = Inaction.DbProvider.Instance.Connection)
            {
                var trans = connection.BeginTransaction();
                foreach (var item in items)
                {
                    item.Save(trans);
                }
                return items;
            }
        }
    }
}
